# -*- coding:utf-8 -*-
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import json, time, sys, os, sets, logging
os.sys.path.append('../utils/constants/')
from constants import *
os.sys.path.append('../utils/memcacheDao//')
import memcacheDao
os.sys.path.append('../modules/comment/')
import commentLogic
os.sys.path.append('../modules/user/')
import userLogic
os.sys.path.append('../modules/message/')
import messageLogic

from tornado.options import define, options
reload(sys)  
sys.setdefaultencoding("utf-8")  


class MessageHandler(tornado.web.RequestHandler):
    def get(self):
        mc = memcacheDao.memcacheDao()
        string_base64 = self.get_argument('code', '')
        user_logic = userLogic.userLogic(self.application.db, mc.getInstance())
        checkin_ret = user_logic.checkin(string_base64)
        if checkin_ret['retcode'] != 0:
            logging.warning('chk:%u' % (checkin['retcode'],))
            ret = checkin_ret
        else:
            strType = self.get_argument('type', '')
            #print self.get_argument('field')
            #print strType
            if strType == 'list':
                # 获取列表
                user_id    = int(checkin_ret['retbody']['user_id'])
                pageno  = int(self.get_argument('pageno', 0))
                message_logic = messageLogic.messageLogic(self.application.db)
                ret = message_logic.get_list(user_id, pageno)
                logging.info('t:%s r:%u' % (strType, ret['retcode'],))
            #elif strType == 'detail':
            #    # 获取详细
            #    user_id    = int(checkin_ret['retbody']['user_id'])
            #    message_id = int(self.get_argument('message_id'))
            #    message_logic = messageLogic.messageLogic(self.application.db)
            #    ret = message_logic.get_content(user_id, message_id,)
            elif strType == 'count':
                # 获取计数
                user_id    = int(checkin_ret['retbody']['user_id'])
                message_logic = messageLogic.messageLogic(self.application.db)
                ret = message_logic.get_count(user_id)
                logging.info('t:%s r:%u' % (strType, ret['retcode'],))
            else:
                # 404
                ret = {}
                ret['retcode'] = E_METHOD_NOT_EXIST
                ret['message'] = 'method not exists'
                logging.warning('t:%s method not exists' % (strType,))
        self.write(json.dumps(ret))

    def post(self):
        mc = memcacheDao.memcacheDao()
        strType = self.get_argument('type', '')
        argument_dict = {}
        ret = {}
        if strType == 'delete':
            string_base64 = self.get_argument('code', '')
            user_logic = userLogic.userLogic(self.application.db, mc.getInstance())
            checkin_ret = user_logic.checkin(string_base64)
            if checkin_ret['retcode'] != 0:
                logging.warning('chk:%u' % (checkin['retcode'],))
                ret = checkin_ret
            else:
                message_logic = messageLogic.messageLogic(self.application.db)
                message_id = int(self.get_argument('message_id'))
                user_id    = int(checkin_ret['retbody']['user_id'])
                ret = message_logic.remove(user_id, message_id)
                logging.info('t:%s r:%u' % (strType, ret['retcode'],))
        elif strType == 'clear':
            string_base64 = self.get_argument('code', '')
            user_logic = userLogic.userLogic(self.application.db, mc.getInstance())
            checkin_ret = user_logic.checkin(string_base64)
            if checkin_ret['retcode'] != 0:
                logging.warning('chk:%u' % (checkin['retcode'],))
                ret = checkin_ret
            else:
                message_logic = messageLogic.messageLogic(self.application.db)
                user_id    = int(checkin_ret['retbody']['user_id'])
                ret = message_logic.clear_unread_count(user_id,)
                logging.info('t:%s r:%u' % (strType, ret['retcode'],))
        else:
            # 404
            ret['retcode'] = E_METHOD_NOT_EXIST
            ret['message'] = 'method not exists'
            logging.warning('t:%s method not exists' % (strType,))
        #print ret
        self.write(json.dumps(ret))
